"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具读取dss文件内容之-o offset参数验证
Create At   : 2023/4/12
Owner       : opentestcase019
Description :
    1.读取dss文件内容，指定-o offset参数为有效值
    2.读取dss文件内容，指定-o offset参数为无效值
Expect      :
    1.成功
    2.合理报错
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:读取dss文件内容，指定-o offset参数为有效值;expect:显示成功-----"
        self.logger.info(step1)
        self.logger.info("-----获取卷组名-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.", execute_msg, "获取卷组名失败")
        vgname = execute_msg.splitlines()[1].split()[0].strip()
        self.logger.info(vgname)
        cmd_list = [f"-p +{vgname}/pg_control -o 2048 -f s",
                    f"-p +{vgname}/pg_control -o '2048' -f s",
                    f"--path +{vgname}/pg_control -o 2048 -f s",
                    f"--path +{vgname}/pg_control -o '2048' -f s"]
        exp = f"filename is +{vgname}/pg_control, offset is 2048."
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd examine {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn(exp, execute_msg, "执行失败" + step1)

        step2 = "-----step2:读取dss文件内容，指定-o offset参数为无效值;expect:合理报错-----"
        self.logger.info(step2)
        cmd_list = [f"-p +{vgname}/pg_control -o '-1' -f s",
                    f"--path +{vgname}/pg_control -o zero -f s",
                    f"--path +{vgname}/pg_control -o 100 -f s",
                    f"--path +{vgname}/pg_control -o *100 -f s",
                    f"--path +{vgname}/pg_control -o #100 -f s"]
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd examine {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            if cmd == cmd_list[4]:
                self.assertIn("-o should with args.",
                              execute_msg, "执行失败" + step2)
            elif cmd == cmd_list[0] or cmd == cmd_list[2]:
                self.assertIn("offset must be >= 0 and be align 512.",
                              execute_msg, "执行失败" + step2)
            else:
                self.assertIn("The value of offset is invalid.",
                              execute_msg, "执行失败" + step2)

    def tearDown(self):
        self.logger.info("-----无需环境清理-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
